from django.db import models

from utils.models import BaseModel
from settings import const_settings


class Stall(BaseModel):
    board = models.ImageField(
        verbose_name='食摊牌匾',
        upload_to='pic/board/',
        default='pic/board/default_board.png'
    )

    @property
    def board_url(self):
        return const_settings.DRF_URL + 'media/' + str(self.board)

    name = models.CharField(
        verbose_name='食摊名称',
        max_length=16,
        unique=True,
    )
    style = models.IntegerField(
        verbose_name='食摊主格',
        choices=(
            (1, '饱餐'),
            (2, '品尝')
        ),
    )
    s_style = models.IntegerField(
        verbose_name='食摊副格',
        choices=(
            (1, '烤熏'),
            (2, '炸煎'),
            (3, '炖炒'),
            (4, '其他'),
        ),
    )
    num = models.CharField(
        verbose_name='食摊编号',
        max_length=16,
        default='0$0$0'
    )

    @property
    def order_id(self):
        return int(self.num.split('$')[2])

    @property
    def zone_id(self):
        return int(self.num.split('$')[1])

    @property
    def region_id(self):
        return int(self.num.split('$')[0])

    sales = models.CharField(
        verbose_name='食摊销量',
        max_length=32,
        default='0$0$0'
    )

    @property
    def quarter_sales(self):
        return int(self.sales.split('$')[0])

    @property
    def month_sales(self):
        return int(self.sales.split('$')[1])

    @property
    def week_sales(self):
        return int(self.sales.split('$')[2])

    user_id = models.IntegerField(
        verbose_name='摊主id',
    )

    state = models.IntegerField(
        verbose_name='食摊状态',
        choices=(
            (1, '未营业'),
            (2, '准备中'),
            (3, '营业中'),
        ),
        default=1
    )

    food_sort = models.CharField(
        verbose_name='小吃分类',
        max_length=96,
        default='分类一$分类二'
    )

    part_sort = models.CharField(
        verbose_name='配件分类',
        max_length=96,
        default='配件一$配件二'
    )

    def __str__(self):
        return '[ pk --- {} ]'.format(self.pk) + self.name + '< ' + self.num.replace('$', '---') + ' >'

    class Meta:
        verbose_name_plural = '食摊表'


class Zone(BaseModel):
    name = models.CharField(
        verbose_name='食区名称',
        max_length=16,
        unique=True,
    )
    num = models.CharField(
        verbose_name='食区编号',
        max_length=16,
        default='0$0'
    )
    axis = models.CharField(
        verbose_name='食区坐标',
        max_length=16,
        default='0$0'
    )

    @property
    def bottom(self):
        return self.axis.split('$')[0] + 'px'

    @property
    def left(self):
        return self.axis.split('$')[1] + 'px'

    @property
    def region_id(self):
        return int(self.num.split('$')[0])

    @property
    def zone_id(self):
        return int(self.num.split('$')[1])

    def __str__(self):
        return self.name + '< ' + self.num.replace('$', '---') + ' >'

    class Meta:
        verbose_name_plural = '食区表'


class Region(BaseModel):
    name = models.CharField(
        verbose_name='城区名称',
        max_length=16,
        unique=True,
    )
    region_id = models.CharField(
        verbose_name='城区编号',
        max_length=16,
    )
    region_map = models.ImageField(
        verbose_name='城区地图',
        upload_to='pic/region_map/',
    )

    @property
    def map_url(self):
        return const_settings.DRF_URL + 'media/' + str(self.region_map)

    def __str__(self):
        return self.name + '< ' + self.region_id + ' >'

    class Meta:
        verbose_name_plural = '城区表'


class Food(BaseModel):
    stall_id = models.IntegerField(
        verbose_name='小吃所属食摊编号'
    )
    sort = models.CharField(
        verbose_name='小吃分类',
        max_length=16
    )

    name = models.CharField(
        verbose_name='小吃名称',
        max_length=16,
    )
    price = models.DecimalField(
        verbose_name='小吃价格',
        max_digits=6,
        decimal_places=2,
    )
    plate = models.ImageField(
        verbose_name='小吃图片',
        upload_to='pic/plate/',
        default='pic/plate/default_plate.png'
    )

    @property
    def plate_url(self):
        return const_settings.DRF_URL + 'media/' + str(self.plate)

    desc = models.CharField(
        verbose_name='小吃描述',
        max_length=192
    )
    month_sales = models.IntegerField(
        verbose_name='小吃本月销量',
        default=0,
    )

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = '小吃表'


class Part(BaseModel):
    stall_id = models.IntegerField(
        verbose_name='配件所属食摊编号'
    )
    sort = models.CharField(
        verbose_name='配件分类',
        max_length=16
    )
    name = models.CharField(
        verbose_name='配件名称',
        max_length=16
    )
    price = models.DecimalField(
        verbose_name='配件价格',
        max_digits=6,
        decimal_places=2,
    )
    part = models.ImageField(
        verbose_name='配件图片',
        upload_to='pic/part/'
    )

    @property
    def part_url(self):
        return const_settings.DRF_URL + 'media/' + str(self.part)

    desc = models.CharField(
        verbose_name='配件描述',
        max_length=192
    )
    order = models.IntegerField(
        verbose_name='配件显示顺序'
    )

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = '配件表'


class Bill(BaseModel):
    bill_num = models.CharField(
        verbose_name='订单流水号',
        max_length=20
    )
    stall_id = models.IntegerField(
        verbose_name='摊位编号'
    )
    user_id = models.IntegerField(
        verbose_name='顾客编号'
    )
    is_paid = models.BooleanField(
        verbose_name='是否支付',
        default=False
    )
    paid_time = models.CharField(
        verbose_name='消费时间',
        max_length=96,
        null=True,
        blank=True,
    )

    def __str__(self):
        return '<{}>号摊位<{}>号顾客的账单'.format(self.stall_id, self.user_id)

    class Meta:
        verbose_name_plural = '账单表'


class Row(BaseModel):
    bill_id = models.IntegerField(
        verbose_name='账单编号',
    )
    food_id = models.IntegerField(
        verbose_name='小吃编号',
        default=0
    )
    part_id = models.IntegerField(
        verbose_name='配件编号',
        default=0
    )
    count = models.IntegerField(
        verbose_name='小吃数量',
        default=1
    )

    def __str__(self):
        sort = '小吃' if self.food_id else '配件'
        id = self.food_id if self.food_id else self.part_id
        return '<{}>号账单的<{}>号<{}><{}>件'. \
            format(self.bill_id, id, sort, self.count)

    class Meta:
        verbose_name_plural = '小吃记录表'
